import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/request/auth'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: () => import('@/views/Index.vue'),
    redirect: '/home',
    meta: { requireAuth: true },
    children: [
      {
        path: '/home',
        component: () => import('@/views/Home.vue'),
        meta: { requireAuth: true }
      },
      {
        path: '/user',
        component: () => import('@/views/table/UserCon.vue'),
        meta: { requireAuth: true }
      },
      {
        path: '/type',
        component: () => import('@/views/table/TypeCon.vue'),
        meta: { requireAuth: true }
      },
      {
        path: '/comment',
        component: () => import('@/views/table/CommentCon.vue'),
        meta: { requireAuth: true }
      },
      {
        path: '/tag',
        component: () => import('@/views/table/TagCon.vue'),
        meta: { requireAuth: true }
      },
      {
        path: '/publish/:id?',
        component: () => import('@/views/ArticlePublish.vue'),
        meta: { requireAuth: true }
      },
      {
        path: '/article',
        component: () => import('@/views/table/ArticleCon.vue'),
        meta: { requireAuth: true }
      },
      {
        path: '/complain',
        component: () => import('@/views/table/ComplainCon.vue'),
        meta: { requireAuth: true }
      },
      {
        path: '/authentication',
        component: () => import('@/views/table/Authentication.vue'),
        meta: { requireAuth: true }
      },
      {
        path: '/information',
        component: () => import('@/views/Information.vue'),
        meta: { requireAuth: true }
      }
    ]
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login.vue")
  },
]

const router = new VueRouter({
  routes
})

//路由守卫
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
    if (getToken()) { //判断本地是否存在access_token
      next();
    } else {
      next({
        path: '/login'
      })
    }
  }
  else {
    next();
  }
  /*如果本地 存在 token 则 不允许直接跳转到 登录页面*/
  // if(to.fullPath == "/login"){
  //   if(getToken()){
  //     next({
  //       path:from.fullPath
  //     });
  //   }else {
  //     next();
  //   }
  // }
});

export default router